﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.yy.manager {
    import com.yy.base.App;
    import com.yy.manager.base.ICursor;
    
    import flash.display.*;
    import flash.events.*;
    import flash.ui.*;

    public class MouseManager {

        private var _stage:Stage;
        private var _parent:DisplayObjectContainer;
        private var _update:Boolean = false;
        private var _preCursor:DisplayObject;
        private var _currentCursor:DisplayObject;
        private var _hideingCursor:String;
        private var _defaultCursor:DisplayObject;
        private var _buttonOverCursor:DisplayObject;
        private var _lock:Boolean = false;

        public function MouseManager(){
            this._stage = App.appStage;
            this._stage.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMoveHandler);
            this._stage.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDownHandler);
            this._stage.addEventListener(MouseEvent.MOUSE_UP, this.mouseUpHandler);
        }
        public function get parent():DisplayObjectContainer{
            return (((this._parent) || (this._stage)));
        }
        public function set parent(_arg1:DisplayObjectContainer):void{
            this._parent = _arg1;
        }
        public function get update():Boolean{
            return (this._update);
        }
        public function set update(_arg1:Boolean):void{
            this._update = _arg1;
        }
        private function _setCurrentCursor(_arg1:DisplayObject):void{
            if (_arg1 == this._currentCursor){
                return;
            };
            if ((_arg1 is DisplayObjectContainer)){
                (_arg1 as DisplayObjectContainer).mouseChildren = false;
            };
            if ((_arg1 is InteractiveObject)){
                (_arg1 as InteractiveObject).mouseEnabled = false;
            };
            if (((!((this._currentCursor == this._defaultCursor))) && (!((this._currentCursor == this._buttonOverCursor))))){
                this._preCursor = this._currentCursor;
            };
            if (((this._currentCursor) && (this.parent.contains(this._currentCursor)))){
                this.parent.removeChild(this._currentCursor);
                this._currentCursor = null;
            };
            if (_arg1){
                this._currentCursor = _arg1;
                this.parent.addChild(this._currentCursor);
                this.mouseMoveHandler();
            };
            if (this._currentCursor){
                this.hideCursor();
            } else {
                this.showCursor();
            };
        }
        public function getCurrentCursor():DisplayObject{
            return (this._currentCursor);
        }
        private function hideCursor():void{
            if (this._hideingCursor){
                return;
            };
            this._hideingCursor = Mouse.cursor;
            Mouse.cursor = MouseCursor.AUTO;
            Mouse.hide();
        }
        private function showCursor():void{
            if (this._hideingCursor){
                Mouse.show();
                Mouse.cursor = this._hideingCursor;
                this._hideingCursor = null;
            };
        }
        public function setCursor(_arg1:DisplayObject, _arg2:Boolean=false):void{
            if ((((this._lock == false)) || ((_arg2 == true)))){
                this._setCurrentCursor(_arg1);
                this._lock = _arg2;
            };
        }
        public function removeCursor(_arg1:DisplayObject=null, _arg2:Boolean=false):void{
            if (_arg1 == null){
                _arg1 = this._currentCursor;
            };
            if (this._currentCursor == _arg1){
                this.unlock();
                if (_arg2){
                    this.setCursor(this._preCursor);
                } else {
                    this.setCursor(null);
                    this._preCursor = null;
                };
            };
        }
        public function unlock():void{
            this._lock = false;
        }
        public function set defaultCursor(_arg1:DisplayObject):void{
            this._defaultCursor = _arg1;
            if (this._currentCursor == null){
                this.setCursor(this._defaultCursor);
            };
        }
        public function set buttonOverCursor(_arg1:DisplayObject):void{
            this._buttonOverCursor = _arg1;
        }
        private function mouseMoveHandler(_arg1:MouseEvent=null):void{
            if (this._currentCursor){
                this._currentCursor.x = this._stage.mouseX;
                this._currentCursor.y = this._stage.mouseY;
            };
            if (this.update){
                _arg1.updateAfterEvent();
            };
        }
        protected function mouseDownHandler(_arg1:MouseEvent):void{
            if ((this._currentCursor is ICursor)){
                (this._currentCursor as ICursor).mouseDown();
            };
        }
        protected function mouseUpHandler(_arg1:MouseEvent):void{
            if ((this._currentCursor is ICursor)){
                (this._currentCursor as ICursor).mouseUp();
            };
        }
        public function mouseOver(_arg1:Object):void{
            if (this._lock){
                return;
            };
            if ((((_arg1 as Sprite).buttonMode) && ((_arg1 as Sprite).useHandCursor))){
                if (this._buttonOverCursor){
                    this.setCursor(this._buttonOverCursor);
                } else {
                    this.setCursor(null);
                    Mouse.cursor = MouseCursor.BUTTON;
                };
            };
        }
        public function mouseOut(_arg1:Object):void{
            if ((((Mouse.cursor == MouseCursor.BUTTON)) || (((!((this._buttonOverCursor == null))) && ((this._currentCursor == this._buttonOverCursor)))))){
                if (this._preCursor == null){
                    if (this._defaultCursor){
                        this.setCursor(this._defaultCursor);
                    } else {
                        this.setCursor(null);
                        if (!this._lock){
                            Mouse.cursor = MouseCursor.AUTO;
                        };
                    };
                } else {
                    this.setCursor(this._preCursor);
                };
            };
        }

    }
}//package com.fminutes.vinciUI.manager 
